package com.hisan.freeride.home.fragment;

import android.os.Bundle;

import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.base.BaseFragment;
import com.hisan.freeride.common.callback.NoDialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.FofferMessageBinding;
import com.hisan.freeride.home.activity.Message_DetailsActivtity;
import com.hisan.freeride.home.adapter.OfferMesageAdapter;
import com.hisan.freeride.home.model.OfferMessage;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/10
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：公益活动页面
 */
public class OfferMessageFragment extends BaseFragment<FofferMessageBinding> {
    private OfferMesageAdapter adapter;
    private int page=1;
    private  List<OfferMessage> messages=new ArrayList<>();
    //统一的Fragment构建方法
    public static OfferMessageFragment newInstance() {
        OfferMessageFragment fragment=null;
        if (fragment==null)fragment = new OfferMessageFragment();
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.foffer_message;
    }

    protected void initView() {
        mBinding.setStateModel(mStateModel);
        adapter=new OfferMesageAdapter(new ArrayList<>());

        mBinding.rv.setAdapter(adapter);
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(getActivity()));
    }

    @Override
    protected void initEvent() {
        adapter.setOnItemClickListener(data -> {
            Bundle bundle=new Bundle();
            bundle.putInt("id",data.getId());
            bundle.putInt("type",1);
            startActivityForResult(Message_DetailsActivtity.class,0,bundle,true);
        });
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                messages.clear();
                loadData(true);
                mBinding.pull.finishRefresh();
            }

            @Override
            public void loadMore() {
                page++;
                loadData(true);
                mBinding.pull.finishLoadMore();
            }
        });
    }
    private void getdata(){
        messages.clear();
        //可能需要移除之前添加的布局
        adapter.removeAllFooterView();

        OkGo.<List<OfferMessage>>get(AppConfig.Article)
                .tag(this)
                .params("page",page)
                .params("size",20)
                .params("city_id", BaseAppLication.getInstance().getCityid())
                .execute(new NoDialogCallback<List<OfferMessage>>(getActivity()) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<List<OfferMessage>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            if (isOk(response)){
                                if(response.body().size()>=20){
                                    mBinding.pull.setCanLoadMore(true);
                                }else {
                                    mBinding.pull.setCanLoadMore(false);
                                }
                                showData();
                                messages.addAll(response.body());
                                adapter.setNewData(messages);
                            }else {
                                showErrorView(response.code());
                            }
                        }else {
                            if (!CollectionUtils.isNullOrEmpty(messages))
                                adapter.setNewData(messages);
                            noData();
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<List<OfferMessage>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }
    @Override
    protected void loadData(boolean isRefresh) {
        messages.clear();
        getdata();
    }


}
